package com.sugon.controller.tool;

import cn.hutool.core.lang.Assert;
import com.sugon.entity.ToolsControlWayEntity;
import com.sugon.service.ToolsControlWayService;
import com.sugon.utils.PageUtils;
import com.sugon.utils.Query;
import com.sugon.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 管控方式Controller
 *
 * @author sugon
 * @date 2019-08-01 09:46:35
 */
@Controller
@RequestMapping("toolscontrolway")
public class ToolsControlWayController {
    @Autowired
    private ToolsControlWayService toolsControlWayService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("toolscontrolway:list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {

        //查询列表数据
        Query query = new Query(params);

        List<ToolsControlWayEntity> toolsControlWayList = toolsControlWayService.queryList(query);
        int total = toolsControlWayService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(toolsControlWayList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("toolscontrolway:info")
    @ResponseBody
    public R info(@PathVariable("id") String id) {
        ToolsControlWayEntity toolsControlWay = toolsControlWayService.queryObject(id);

        return R.ok().put("toolsControlWay", toolsControlWay);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("toolscontrolway:save")
    @ResponseBody
    public R save(@RequestBody ToolsControlWayEntity toolsControlWay) {
        // 管控代码不能重复
        if (toolsControlWayService.queryObjectByControlCode(toolsControlWay.getControlCode()) != null) {
            return R.error("管控代码不允许重复！");
        }
        return toolsControlWayService.save(toolsControlWay) > 0 ? R.ok() : R.error();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("toolscontrolway:update")
    @ResponseBody
    public R update(@RequestBody ToolsControlWayEntity toolsControlWay) {
        String id = toolsControlWay.getId();
        Assert.notNull(id,"更新工装管控方式时ID未上传！");
        ToolsControlWayEntity toolsControlWayInDB = toolsControlWayService.queryObject(id);
        if(toolsControlWayInDB == null){
            return R.error("工装管控方式不存在，请确认！");
        }
        // 管控代码不能重复
        String controlWayCode = toolsControlWay.getControlCode();
        if (!toolsControlWayInDB.getControlCode().equals(controlWayCode) && toolsControlWayService.queryObjectByControlCode(controlWayCode) != null) {
            return R.error("管控代码不允许重复！");
        }
        return toolsControlWayService.update(toolsControlWay) > 0 ? R.ok() : R.error();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("toolscontrolway:delete")
    @ResponseBody
    public R delete(@RequestBody String[] ids) {
        toolsControlWayService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<ToolsControlWayEntity> list = toolsControlWayService.queryList(params);

        return R.ok().put("list", list);
    }
}
